﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using Coevery.AppFramework.Utils;
using System.IO;
using DevExpress.XtraPrinting;

namespace Coevery.Win.InmuebleModule.Report
{
    public partial class InmuebleReport : DevExpress.XtraReports.UI.XtraReport
    {

        public event EventHandler<EventArgs<InmuebleIdealistaOverview>> NameCellClicked;

        public void OnNameCellClicked(EventArgs<InmuebleIdealistaOverview> e)
        {
            EventHandler<EventArgs<InmuebleIdealistaOverview>> handler = NameCellClicked;
            if (handler != null) handler(this, e);
        }

        public InmuebleReport()
        {
            InitializeComponent();
            this.Detail.PageBreak = DevExpress.XtraReports.UI.PageBreak.None;
            this.Detail1.PageBreak = DevExpress.XtraReports.UI.PageBreak.None;
            this.DetailReportBand.PageBreak = DevExpress.XtraReports.UI.PageBreak.None;
            this.Landscape = false;

            // Bind campos master
            this.comunidadautonomaid.DataBindings.Add(new XRBinding("Text", null, "comunidadautonomaid")); 
            this.provinciaid.DataBindings.Add(new XRBinding("Text", null, "provinciaid"));
            this.codigopostal.DataBindings.Add(new XRBinding("Text", null, "codigopostal"));
            this.municipioid.DataBindings.Add(new XRBinding("Text", null, "municipioid"));
            this.barrio.DataBindings.Add(new XRBinding("Text", null, "barrio"));
            this.tipoviaid.DataBindings.Add(new XRBinding("Text", null, "tipoviaid"));
            this.xrLabel7.DataBindings.Add(new XRBinding("Text", null, "NombreVia"));
            this.numero.DataBindings.Add(new XRBinding("Text", null, "numero"));
            this.planta.DataBindings.Add(new XRBinding("Text", null, "planta"));
            this.puerta.DataBindings.Add(new XRBinding("Text", null, "puerta"));
            this.PrecioMedio.DataBindings.Add(new XRBinding("Text", null, "PrecioMedio"));
            this.TipoVivienda.DataBindings.Add(new XRBinding("Text", null, "TipoVivienda"));
            this.Catastro.DataBindings.Add(new XRBinding("Text", null, "Catastro"));
            this.Metros.DataBindings.Add(new XRBinding("Text", null, "Metros"));
            this.Habitaciones.DataBindings.Add(new XRBinding("Text", null, "Habitaciones"));
            this.Banios.DataBindings.Add(new XRBinding("Text", null, "Banios"));
            this.Estado.DataBindings.Add(new XRBinding("Text", null, "Estado"));
            this.AnioConstruccion.DataBindings.Add(new XRBinding("Text", null, "AnioConstruccion"));
            this.AnioReforma.DataBindings.Add(new XRBinding("Text", null, "AnioReforma"));
            this.Amueblado.DataBindings.Add(new XRBinding("Text", null, "Amueblado"));
            this.CocinaEquipada.DataBindings.Add(new XRBinding("Text", null, "CocinaEquipada"));
            this.Garaje.DataBindings.Add(new XRBinding("Text", null, "Garaje"));
            this.Jardin.DataBindings.Add(new XRBinding("Text", null, "Jardin"));
            this.Aire.DataBindings.Add(new XRBinding("Text", null, "Aire"));
            this.Calefaccion.DataBindings.Add(new XRBinding("Text", null, "Calefaccion"));
            this.Ascensor.DataBindings.Add(new XRBinding("Text", null, "Ascensor"));
            this.Trastero.DataBindings.Add(new XRBinding("Text", null, "Trastero"));
            this.Latitud.DataBindings.Add(new XRBinding("Text", null, "Latitud"));
            this.Longitud.DataBindings.Add(new XRBinding("Text", null, "Longitud"));
            this.Precio.DataBindings.Add(new XRBinding("Text", null, "Precio"));
            this.PrecioMedio.DataBindings.Add(new XRBinding("Text", null, "PrecioMedio"));
            
            
            // Bind campos reporte detalle
            DetailReportBand.DataMember = "InmuebleIdealistas";
            this.Province.DataBindings.Add(new XRBinding("Text", null, "InmuebleIdealistas.Province"));
            this.Munipality.DataBindings.Add(new XRBinding("Text", null, "InmuebleIdealistas.Munipality"));
            this.Address.DataBindings.Add(new XRBinding("Text", null, "InmuebleIdealistas.Address"));
            this.District.DataBindings.Add(new XRBinding("Text", null, "InmuebleIdealistas.District"));
            this.PropertyType.DataBindings.Add(new XRBinding("Text", null, "InmuebleIdealistas.PropertyType"));
            this.Floor.DataBindings.Add(new XRBinding("Text", null, "InmuebleIdealistas.Floor"));
            this.Sise.DataBindings.Add(new XRBinding("Text", null, "InmuebleIdealistas.Size"));
            this.Rooms.DataBindings.Add(new XRBinding("Text", null, "InmuebleIdealistas.Rooms"));
            this.Bathrooms.DataBindings.Add(new XRBinding("Text", null, "InmuebleIdealistas.Bathrooms"));
            this.Price.DataBindings.Add(new XRBinding("Text", null, "InmuebleIdealistas.Price"));
            this.Latitude.DataBindings.Add(new XRBinding("Text", null, "InmuebleIdealistas.Latitude"));
            this.AveragePrice.DataBindings.Add(new XRBinding("Text", null, "InmuebleIdealistas.AveragePrice"));
            this.Longitude.DataBindings.Add(new XRBinding("Text", null, "InmuebleIdealistas.Longitude"));
            
            //Barrio2.BeforePrint += new System.Drawing.Printing.PrintEventHandler(projectNameCell_BeforePrint);
            //Barrio2.PreviewClick += new PreviewMouseEventHandler(projectNameCell_PreviewClick);
        }

        void projectNameCell_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ((XRTableCell)sender).Tag = GetCurrentRow();
        }

        void projectNameCell_PreviewClick(object sender, PreviewMouseEventArgs e)
        {
            var overviewItem = (InmuebleIdealistaOverview)e.Brick.Value;
            OnNameCellClicked(new EventArgs<InmuebleIdealistaOverview>(overviewItem));
        }

        private XRTable CreateTable(int rows, int columns)
        {
            DevExpress.XtraReports.UI.XRTable tbl = new XRTable();
            tbl.BorderColor = Color.White;
            tbl.BorderWidth = 3;
            tbl.Borders = BorderSide.All;
            tbl.BorderDashStyle = DevExpress.XtraPrinting.BorderDashStyle.Solid;
            //DevExpress.XtraReports.UI.XRBarCode xrBarCode = new XRBarCode();

            Detail1.Controls.Add(tbl);

            tbl.Borders = (DevExpress.XtraPrinting.BorderSide)
                (((DevExpress.XtraPrinting.BorderSide.Left
                    | DevExpress.XtraPrinting.BorderSide.Top)
                    | DevExpress.XtraPrinting.BorderSide.Right)
                    | DevExpress.XtraPrinting.BorderSide.Bottom);


            // Total number of rows.
            int rowCnt;
            // Current row count.
            int rowCtr;
            // Total number of cells per row (columns).
            int cellCtr;
            // Current cell counter
            int cellCnt;

            rowCnt = rows;
            cellCnt = columns;

            for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
            {
                // Create new row..
                DevExpress.XtraReports.UI.XRTableRow row = new XRTableRow();
                row.CanGrow = true;
                for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
                {
                    // Create a new cell and add it to the row.
                    DevExpress.XtraReports.UI.XRTableCell cell = new XRTableCell();
                    cell.CanGrow = true;
                    row.Cells.Add(cell);
                }
                // ..and add it to the table.
                tbl.Rows.Add(row);
            }
            
            tbl.CanGrow = true;
            return tbl;
        }

        private void XRPanel3_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            XRPanel panel = sender as XRPanel;
            panel.Controls.Clear();
            this.CanGrow = true;
            
            //InmuebleIdealistaOverview inmIdeal = (InmuebleIdealistaOverview)this.GetCurrentRow();
            InmuebleIdealistaOverview inmIdeal = (InmuebleIdealistaOverview)this.DetailReportBand.GetCurrentRow();
            if (inmIdeal == null) return; 
            FotoesCollection fotos = inmIdeal.Fotoes;
            //int index = 0;            
            
            int columns = 5;
            int rows = ((fotos.Count) / columns) + ((fotos.Count % columns == 0 ? 0 : 1));
            
            for(int index = 0; index < fotos.Count; index++){
                XRPictureBox picBox = new XRPictureBox();
                picBox.Sizing = ImageSizeMode.CenterImage;
                Image img = Image.FromStream(new MemoryStream((((FotoOverview)fotos[index]).Img)));
                picBox.Image = img;
                picBox.SizeF = new SizeF(158F, 158F);
                picBox.BoundsF = new RectangleF(((index % columns) * 164), 
                                                ((index / columns) * 164) + (index / columns), 
                                                158, 158);
                panel.Controls.Add(picBox);
            }
          
        }
    }
}
